package com.ikas.ai.utils;

import java.util.Locale;
import java.util.ResourceBundle;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 资源包
 * @author HXH
 */
public final class ResourceBundleUtil {

    private static final ConcurrentHashMap<String, ResourceBundleUtil> RB_MAP = new ConcurrentHashMap<String, ResourceBundleUtil>();

    private ResourceBundle rb;

    private ResourceBundleUtil(ResourceBundle rb) {
        this.rb = rb;
    }

    /**
     * 多线程下安全
     * @param baseName
     * @return
     */
    public static ResourceBundleUtil getInstance(String baseName) {
        if (RB_MAP.get(baseName) != null) {
            return RB_MAP.get(baseName);
        }

        ResourceBundle resourceBundle = ResourceBundle.getBundle(baseName, Locale.getDefault());
        ResourceBundleUtil resourceBundleUtil = new ResourceBundleUtil(resourceBundle);
        resourceBundleUtil = RB_MAP.putIfAbsent(baseName.toUpperCase(), resourceBundleUtil);
        return resourceBundleUtil;
    }

    public String getText(String key) {
        return rb.getString(key);
    }

}
